<template>
  <div class="app-container">
    <div class="view-title">商品类型管理</div>
    <el-button
      type="primary"
      @click="handlerAdd"
      v-permission="['productType:add']"
      >新增</el-button
    >

    <el-table
      :data="productTableData"
      v-loading="loading"
      element-loading-text="正在加载中"
      :header-cell-style="{
        borderColor: '#F0F0F0',
        height: '51px',
        background: '#F0F0F0',
        color: '#333',
      }"
      :row-style="{ height: '51px' }"
      :cell-style="{ borderColor: '#F0F0F0' }"
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column prop="id" label="类型id" width="180" align="center">
      </el-table-column>
      <el-table-column prop="name" label="类型名称" align="center">
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间" align="center">
      </el-table-column>
      <el-table-column prop="updated_at" label="更新时间" align="center">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button
            @click="handleEdit(scope.row)"
            type="text"
            size="small"
            v-permission="['productType:edit']"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="handlerDelete(scope.row)"
            v-permission="['productType:delete']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div class="page_box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNow"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 添加弹窗 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="addDialogVisible"
      width="30%"
      @close="handleCloseDialog"
    >
      <div class="dialog_add_box">
        <div class="add_title">类型名称：</div>
        <div>
          <el-input
            v-model="addProductValue"
            placeholder="请输入类型名称"
          ></el-input>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelDialogAdd">取 消</el-button>
        <el-button type="primary" @click="sureDialogAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getAppPackageTypeList,
  addAppPackageType,
  updateAppPackageType,
  deleteAppPackageType,
} from "@/api/productType.js";
export default {
  data() {
    return {
      pageNow: 1,
      pageSize: 10,
      total: null,
      productTableData: [],
      addDialogVisible: false,
      addProductValue: "",
      deleteId: "",
      editId: "",
      loading: true,
      dialogTitle: "",
    };
  },
  mounted() {
    this.getAppPackageTypeList();
  },
  methods: {
    // 获取商品类型
    getAppPackageTypeList() {
      getAppPackageTypeList({
        page_now: this.pageNow,
        page_size: this.pageSize,
      }).then((res) => {
        if (res.code == 1) {
          this.loading = false;
          this.productTableData = res.data;
          this.total = res.page_info.total_count;
        } else {
          this.loading = false;
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 添加商品类型
    addAppPackageType() {
      addAppPackageType({
        name: this.addProductValue,
      }).then((res) => {
        if (res.code == 1) {
          this.$message.success(res.msg);
          this.addDialogVisible = false;
          this.getAppPackageTypeList();
          this.addProductValue = "";
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 删除商品类型
    deleteAppPackageType() {
      deleteAppPackageType({
        ids: this.deleteId,
      }).then((res) => {
        if (res.code == 1) {
          this.$message.success(res.msg);
          this.getAppPackageTypeList();
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 编辑商品类型
    updateAppPackageType() {
      updateAppPackageType({
        id: this.editId,
        name: this.addProductValue,
      }).then((res) => {
        if (res.code == 1) {
          this.$message.success(res.msg);
          this.addDialogVisible = false;
          this.getAppPackageTypeList();
          this.addProductValue = "";
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 编辑
    handleEdit(val) {
      this.dialogTitle = "编辑";
      this.editId = val.id;
      this.addProductValue = val.name;
      this.addDialogVisible = true;
    },

    // 删除
    handlerDelete(val) {
      this.deleteId = val.id;
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteAppPackageType();
        })
        .catch(() => {});
    },

    // 添加
    handlerAdd() {
      this.dialogTitle = "添加";
      this.addDialogVisible = true;
    },

    // 添加弹窗-叉号
    handleCloseDialog() {
      this.addDialogVisible = false;
      this.addProductValue = "";
    },

    // 添加弹窗确定
    sureDialogAdd() {
      if (this.editId) {
        this.updateAppPackageType();
      } else {
        this.addAppPackageType();
      }
    },

    // 添加弹窗取消
    cancelDialogAdd() {
      this.addDialogVisible = false;
      this.addProductValue = "";
    },

    // 分页
    handleSizeChange(val) {
      this.pageSize = val;
      this.getAppPackageTypeList();
    },
    handleCurrentChange(val) {
      this.pageNow = val;
      this.pageSize = 10;
      this.getAppPackageTypeList();
    },
  },
};
</script>
<style lang="scss" scoped>
.dialog_add_box {
  display: flex;
  align-items: center;
}
</style>
